﻿using AJWPFAdmin.Core;
using AJWPFAdmin.Core.Components.AJTable.ViewModels;
using AJWPFAdmin.Core.Components.Views;
using AJWPFAdmin.Core.Excel;
using AJWPFAdmin.Core.ExtensionMethods;
using AJWPFAdmin.Core.Models;
using AJWPFAdmin.Core.Models.EF.Tables;
using AJWPFAdmin.Core.Mvvm;
using AJWPFAdmin.Core.Utils;
using AJWPFAdmin.Services.EF;
using MaterialDesignExtensions.Controls;
using MaterialDesignThemes.Wpf;
using Microsoft.AspNetCore.Mvc;
using Prism.Commands;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using AJWPFAdmin.Core.Components.AJFilePicker.ViewModels;
using Prism.Mvvm;

namespace AJWPFAdmin.Modules.Common.ViewModels
{
    public class PromptDialogViewModel : ViewModelBase, IDialogAware
    {
        public class PromptDialogParameter : BindableBase
        {
            private string _title;
            public string Title
            {
                get => _title;
                set => SetProperty(ref _title, value);
            }

            private string _placeholder;
            public string Placeholder
            {
                get => _placeholder;
                set => SetProperty(ref _placeholder, value);
            }

            private int _maxLength;
            public int MaxLength
            {
                get => _maxLength;
                set => SetProperty(ref _maxLength, value);
            }

            private bool _required;
            public bool Required
            {
                get => _required;
                set => SetProperty(ref _required, value);
            }

            private bool _singleLine;
            public bool SingleLine
            {
                get => _singleLine;
                set => SetProperty(ref _singleLine, value);
            }

            public PromptDialogParameter()
            {
                Title = "请输入";
                SingleLine = true;
                Placeholder = "请输入内容";
                MaxLength = 100;
            }
        }

        public event Action<IDialogResult> RequestClose;

        public string Title => "";

        private PromptDialogParameter _params;
        public PromptDialogParameter Params
        {
            get => _params;
            set => SetProperty(ref _params, value);
        }

        private string _value;
        public string Value
        {
            get => _value;
            set => SetProperty(ref _value, value);
        }

        private string _info;
        public string Info
        {
            get => _info;
            set => SetProperty(ref _info, value);
        }

        private DelegateCommand<bool?> _confirmCmd;
        public DelegateCommand<bool?> ConfirmCmd =>
            _confirmCmd ?? (_confirmCmd = new DelegateCommand<bool?>(ExecuteConfirmCmd));

        void ExecuteConfirmCmd(bool? parameter)
        {
            if (!parameter.GetValueOrDefault())
            {
                RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
                return;
            }

            if (Params.Required && string.IsNullOrWhiteSpace(Value))
            {
                Info = "该项必填";
                return;
            }

            RequestClose?.Invoke(new DialogResult(ButtonResult.OK, new DialogParameters
            {
                { "value", Value },
            }));
        }

        public PromptDialogViewModel()
        {

        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            parameters.TryGetValue<PromptDialogParameter>("params", out var @params);
            @params ??= new PromptDialogParameter();
            Params = @params;
        }
    }
}
